package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.jackson.JsonUtil;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.OrderLogistics;
import cn.signalpha.modules.shop.entity.OrderLogisticsDetail;
import cn.signalpha.modules.shop.mapper.OrderLogisticsMapper;
import cn.signalpha.modules.shop.vo.OrderGoodsVO;
import cn.signalpha.modules.shop.vo.OrderLogisticsVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static cn.signalpha.common.cache.CacheNames.EXPIRATION_TIME_7;
import static cn.signalpha.common.cache.CacheNames.ORDER_LOGISTICS;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
@AllArgsConstructor
public class OrderLogisticsService extends ServiceImpl<OrderLogisticsMapper, OrderLogistics> {

	private final OrderLogisticsDetailService orderLogisticsDetailService;

	/**
	 * 自定义分页
	 */
    public IPage<OrderLogisticsVO> selectOrderLogisticsPage(IPage<OrderLogisticsVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectOrderLogisticsPage(page, params));
    }

	/**
	 * 自定义列表
	 */
	public List<OrderLogisticsVO> selectOrderLogisticsList(Map<String, Object> params) {
		return baseMapper.selectOrderLogisticsPage(null, params);
	}

    /**
	 * 自定义Wrappers
	 */
    public QueryWrapper<OrderLogistics> selectOrderLogisticsWrapper(Map<String, Object> params) {
        QueryWrapper<OrderLogistics> wrapper = Wrappers.query();
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("id")), OrderLogistics::getId, params.get("id"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("orderNo")), OrderLogistics::getOrderNo, params.get("orderNo"));
		wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("logisticsId")), OrderLogistics::getLogisticsId, params.get("logisticsId"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("logisticsNo")), OrderLogistics::getLogisticsNo, params.get("logisticsNo"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("createTime")), OrderLogistics::getCreateTime, params.get("createTime"));
        wrapper.lambda().orderByDesc(OrderLogistics::getId);
        return wrapper;
    }

	/**
	 * 获取订单物流信息
	 */
	@Cacheable(cacheNames = ORDER_LOGISTICS + EXPIRATION_TIME_7, key = "#orderNo", unless="#result==null")
	public List<OrderLogisticsVO> listByOrderNo(String orderNo) {
		List<OrderLogisticsVO> orderLogisticsList = selectOrderLogisticsList(Kv.create().set("orderNo", orderNo));
		orderLogisticsList.forEach(orderLogistics -> {
			List<OrderLogisticsDetail> orderLogisticsDetailList = orderLogisticsDetailService.list(Wrappers.<OrderLogisticsDetail>lambdaQuery()
				.eq(OrderLogisticsDetail::getLogisticsId, orderLogistics.getId())
			);
			orderLogisticsDetailList.forEach(orderLogisticsDetail -> {
				orderLogisticsDetail.setGoods(JsonUtil.parse(orderLogisticsDetail.getGoodsDetail(), OrderGoodsVO.class));
			});
			orderLogistics.setDetailList(orderLogisticsDetailList);
		});
		return orderLogisticsList;
	}

	/**
	 * 删除订单物流信息缓存
	 */
	@CacheEvict(cacheNames = ORDER_LOGISTICS, key = "#orderNo")
	public void removeCacheByOrderNo(String orderNo) {
	}

	/**
	 * 订单号删除
	 */
	public void removeByOrderNo(String orderNo) {
		baseMapper.delete(Wrappers.<OrderLogistics>lambdaQuery().eq(OrderLogistics::getOrderNo, orderNo));
	}


}

